﻿/*
Copyright (C) 2007 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// ---------------------- Default Parsers ------------------------

// Generates sequential IDs.
//
// Some feeds do not provide entry IDs, however the gadget needs each entry
// to have a unique ID. When parsing a feed and encountering an ID-less entry,
// use this to create an ID.
var IdGenerator = new function() {
  this.nextId = 0;

  this.getNextId = function() {
    return this.nextId++;
  }
}

// Attempts to automatically parse feed.
// So far can detect and parse basic ATOM and RSS.
//
// A parsed entry is a JS object and contains these fields:
//
// title - The entry title.
// content - The description or summary.
// link - The link to the entry.
//
// And may contain values for these fields:
//
// published - Published date.
// id - Unique identifier for the entry.
//
// When finished, executes callback with array of parsed entries.
function parseFeedDefault(responseText, callback) {
  debug.trace('Default feed parsing.');

  var doc;

  try {
    doc = new DOMDocument();
    doc.loadXML(responseText);
  } catch(e) {
    debug.error('Could not load XML.');
    return;
  }

  // Get the top-level tag name.
  var rootTagName = doc.documentElement.tagName;

  var entries;

  if (rootTagName == 'rss' || rootTagName =='channel') {
    entries = parseRSS(doc, CONFIG_MAX_ENTRIES);
  } else if (rootTagName == 'feed') {
    // ATOM has top level "feed" element.
    entries = parseATOM(doc, CONFIG_MAX_ENTRIES);
  } else {
    debug.error('Unrecognized feed type.');
    return;
  }

  callback(entries);
}

// Generic RSS parser.
// Returns array of data objects.
function parseRSS(doc, maxEntries) {
  debug.trace('Parsing RSS feed.');
  var entries = [];

  try {
    var entryElements = doc.getElementsByTagName('item');

    for (var i = 0; i < entryElements.length && i < maxEntries; ++i) {
      var entry = entryElements[i];
      var entryData = {};

      // ID.
      if (entry.getElementsByTagName('guid').length > 0) {
        entryData['id'] = entry.getElementsByTagName('guid')[0].text;
      } else {
        entryData['id'] = IdGenerator.getNextId();
      }

      // Entry title.
      entryData['title'] = entry.getElementsByTagName('title')[0].text;
			//view.alert(entryData['title']);
			var title_split = entryData['title'].split("=");
			var split_val_with_brace = title_split[1].split("(");
			//view.alert(split_val_with_brace[0]);
			var final_val = split_val_with_brace[1].split(")");
			//view.alert(final_val[0]);
			//view.alert(entryData['title']);
			//view.alert(split_val_with_brace[0]==" INR ");
			
					
			if(split_val_with_brace[0]==" AUD ")
			{
				country_array[0]=final_val[0];
				
			}
			if(split_val_with_brace[0]==" BRL ")
			{
				country_array[1]=final_val[0];
				
			}
			if(split_val_with_brace[0]==" CAD ")
			{
				country_array[2]=final_val[0];
				
			}
		if(split_val_with_brace[0]==" EUR ")
			{
				country_array[3]=final_val[0];
				
			}
		if(split_val_with_brace[0]==" INR ")
			{
				country_array[4]=final_val[0];
				
			}
		if(split_val_with_brace[0]==" JPY ")
			{
				country_array[5]=final_val[0];
				
			}
	if(split_val_with_brace[0]==" USD ")
			{
				country_array[6]=final_val[0];
				
			}
			if(split_val_with_brace[0]==" GBP ")
			{
				country_array[7]=final_val[0];
				
			}
			
      // Strip any possible HTML from title.
      entryData['title'] = stripHtml(entryData['title']);
		
      
			// Publish date.
      if (entry.getElementsByTagName('pubDate').length > 0) {
        var pubDate = entry.getElementsByTagName('pubDate')[0].text;
        entryData['published'] = new Date(pubDate);
      }

      // Entry content.
      entryData['content'] = entry.getElementsByTagName('description')[0].text;

      // Link.
      entryData['link'] = entry.getElementsByTagName('link')[0].text;

      entries.push(entryData);
    }
  } catch(e) {
    debug.error('Error parsing RSS feed: ' + e.message);
    return;
 }

  debug.trace('Successfully parsed ' + entries.length + ' entries.');

  return entries;
}

// Generic ATOM parser.
// Returns array of data objects.
function parseATOM(doc, maxEntries) {
  debug.trace('Parsing ATOM feed.');
  var entries = [];

  try {
    var entryElements = doc.getElementsByTagName('entry');

    for (var i = 0; i < entryElements.length && i < maxEntries; ++i) {
      var entry = entryElements[i];
      var entryData = {};

      // ID.
      entryData['id'] = entry.getElementsByTagName('id')[0].text;

      // Entry title.
      entryData['title'] = entry.getElementsByTagName('title')[0].text;

      // Strip any possible HTML from title.
      entryData['title'] = stripHtml(entryData['title']);

      // Published.
      var published = entry.getElementsByTagName('published')[0].text;
      entryData['published'] = parseRFC3339(published);

      // Entry content.
      entryData['content'] = entry.getElementsByTagName('content')[0].text;

      // Get the alt link.
      var linkElements = entry.getElementsByTagName('link');

      for (var j = 0; j < linkElements.length; ++j) {
        var link = linkElements[j];
        var rel = link.getAttribute('rel');

        if (rel == 'alternate') {
          entryData['link'] = link.getAttribute('href');
        }
      }

      entries.push(entryData);
    }
  } catch(e) {
    debug.error('Error parsing ATOM feed: ' + e.message);
    return;
  }

  debug.trace('Successfully parsed ' + entries.length + ' entries.');

  return entries;
}
